/**
 * @brief Timer stuff implementation
 * @author Gilad Avidov
 *
 * uses 16bit timer and interrupt count to calculate time elapsed
 */
#include <mcs51/8051.h>
#include <mcs51/at89c51ed2.h>  //also includes 8052.h and 8051.h
#include <mcs51reg.h>
#include <stdio.h>
#include "spi.h"

volatile unsigned char spi_data;
volatile bit spi_data_is_done= 0;


void spi_config(bit cpol, bit cpha, spi_baud_rate_divisor_t baud)
{
    unsigned char config = MSTR | SSDIS | SPEN;
    if (cpol)
        config |= CPOL;
    if (cpha)
        config |= CPHA;
    if (baud & 0x1)
        config |= SPR0;
    if (baud & 0x2)
        config |= SPR1;
    if (baud & 0x3)
        config |= SPR2;

    SPCON = config;
    IEN1 |= 0x04;   /* enable spi interrupt */
    EA=1;           /* enable interrupts */
}

void spi_write(unsigned char byte)
{
    spi_data_is_done = 0;
    SPDAT = byte;
}

unsigned char spi_read()
{
    while (!spi_data_is_done);
    return spi_data;
}
